package com.ruicar.afs.cloud.risk.tortoise.components;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

import java.beans.PropertyDescriptor;

/**
 * @Description
 * @Author Peng Jinbo
 * @Date 2020/08/03 16:27
 */
@Data
@AllArgsConstructor
@Builder
public class GwtRefDataField {

    /**
     * Property descriptor
     */
    private PropertyDescriptor propertyDescriptor;
    /**
     * Json key
     */
    private String jsonKey;
    /**
     * Dic key
     */
    private String dicKey;
    /**
     * Default val
     */
    private String defaultVal;
    /**
     * Translated key
     */
    private String translatedKey;
    /**
     * Use name
     */
    private boolean useName;
    /**
     * Entity
     */
    private boolean entity;

    /**
     * List
     */
    private boolean list;

    /**
     * List parameter type
     */
    private Class<?> listParameterType;

    private String whetherDesc;

    private boolean translatedByKey;

    /**
     * Author Peng Jinbo
     * Write prop object
     *
     * @param obj obj
     * @param val val
     * @return the object
     * @throws Exception exception
     */
    public Object writeProp(Object obj,Object val) throws Exception{
        this.getPropertyDescriptor().getWriteMethod().invoke(obj,val);
        return obj;
    }

    /**
     * Author Peng Jinbo
     * Read prop object
     *
     * @param obj obj
     * @return the object
     * @throws Exception exception
     */
    public Object readProp(Object obj) throws Exception{
        return this.getPropertyDescriptor().getReadMethod().invoke(obj);
    }

    /**
     * Author Peng Jinbo
     * Get name string
     *
     * @return the string
     */
    public String getName(){
        return this.getPropertyDescriptor().getName();
    }
}